package com.mgg.payment.service.pay.impl;

import java.math.BigDecimal;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;

import com.mgg.common.client.message.MessageClient;
import com.mgg.common.dto.OrderDTO;
import com.mgg.common.util.DateUtils;
import com.mgg.payment.service.account.AccountService;
import com.mgg.payment.service.log.LogService;
import com.mgg.payment.service.order.DeliveryOrderService;
import com.mgg.payment.service.order.SalesOrderService;
import com.mgg.payment.service.pay.PaymentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("paymentService")
public class PaymentServiceImpl implements PaymentService{
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Autowired
    private SalesOrderService salesOrderService;

	@Autowired
    private DeliveryOrderService deliveryOrderService;
	
	@Autowired
    private AccountService accountService;
	
	@Autowired
    private LogService logService;
	
	@Autowired
    @Qualifier("poolTaskExecutor")
    private Executor executor;
	
	@Autowired
	private MessageClient messageClient;
	
	@Transactional
	@Override
	public void paySuccess(OrderDTO order, String payWay, String tradeNo) {
		//2.修改订单状态
		String payTime = DateUtils.getTime();
		String userId = order.getUserId();
		String orderId = order.getOrderId();
		salesOrderService.updateOrder(orderId, payWay, tradeNo, payTime);
		
		if (5 == order.getOrderType()) {
			// 开店大礼包订单	
			CompletableFuture
			.runAsync(() ->
			{
				try {
					accountService.processesRegistShopkeeperOrder(userId,order);
					log.info("开店大礼包订单处理成功， orderId = {}", orderId);
					logService.saveOrderLogInfo(orderId, "6", "1", "开店大礼包订单处理成功");
				} catch (Exception e) {
					log.error(e.getMessage(), e);
					log.error("开店大礼包订单处理失败， orderId = {}", orderId);
					logService.saveOrderLogInfo(orderId, "6", "0", "开店大礼包订单处理失败");
				}
			}
			, executor);
		} else {	
			//3.修改账户余额
			BigDecimal amount = order.getAmount();
			if (amount.compareTo(new BigDecimal(0)) > 0) {			
				accountService.updateAccount(userId,null,amount.negate(),null,2,orderId,"销售支出,订单号：" + orderId);
				logService.saveOrderLogInfo(orderId, "5", "1", "修改账户余额");
			}
			
			//结算返利
			CompletableFuture
			.runAsync(() ->
			{
				try {
					accountService.allocateRebateAmount(order);
					log.info("订单返利成功， orderId = {}", orderId);
					logService.saveOrderLogInfo(orderId, "1", "1", "订单返利成功");
				} catch (Exception e1) {
					log.error(e1.getMessage(), e1);
					log.error("订单返利失败， orderId = {}", orderId);
				}
			}
			, executor);
		}
		
		//记录店铺销售数据
		CompletableFuture
		.runAsync(() -> {
			try {
				accountService.updateShopSales(order);
				log.info("记录店铺销售数据成功， orderId = {}", orderId);
				logService.saveOrderLogInfo(orderId, "2", "1", "记录店铺销售数据成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				log.error("记录店铺销售数据失败， orderId = {}", orderId);
			}
		}, executor);

    	CompletableFuture
			.runAsync(() -> {
				try {
					if (deliveryOrderService.isWmsDo(orderId)) {						
						deliveryOrderService.createDeliveryOrder(orderId, payTime);
						log.info("DO单创建成功， soCode = {}, result = {}", orderId, "");					
						logService.saveOrderLogInfo(orderId, "4", "1", "DO单创建成功");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
					log.error("DO单创建失败， soCode = {}", orderId);
					logService.saveOrderLogInfo(orderId, "4", "0", "DO单创建失败");
				}
			}, executor);
    	
		//发送短信
		CompletableFuture.runAsync(() -> { messageClient.sendOrderNotice(userId, orderId); }, executor);
	}
}
